package com.uniview.web.contorller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uniview.base.BaseController;
import com.uniview.entity.po.LoginLog;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.Query;
import com.uniview.entity.vo.QueryBuilder;
import com.uniview.entity.vo.TableJsonVO;
import com.uniview.enums.EMatchMode;
import com.uniview.enums.EOrderMode;
import com.uniview.service.LoginLogService;

@Controller
@RequestMapping(value="/log")
public class LoginLogController extends BaseController {
	
	Logger logger = Logger.getLogger(LoginLogController.class);
	
	@Autowired
	private LoginLogService loginLogService;


	/**
	 * 查询用户登录日志
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/findLoginLogByPage")
	@ResponseBody
	public TableJsonVO findLoginLogByPage(HttpServletRequest request){
		try{
		int page = ServletRequestUtils.getIntParameter(request, "page", 0);
		int limit = ServletRequestUtils.getIntParameter(request, "limit", 5);
		String userName = ServletRequestUtils.getStringParameter(request, "userName");
		QueryBuilder queryBuilder = new QueryBuilder(LoginLog.class.getSimpleName());
			//logger.info("===========编码前：" +userName);
		if(userName!=null && !"".equals(userName)) {
			/*userName = new String(userName.getBytes("iso-8859-1"), "utf-8");
			logger.info("===========编码后：" +userName);*/
			queryBuilder.add(new Query("userName", EMatchMode.LIKE, userName));
		}
		queryBuilder.setOrderCols("loginTime");
		queryBuilder.setOrderMode(EOrderMode.DESC);
		Pager<LoginLog> pager = loginLogService.findLoginLogByPager(queryBuilder, page, limit);
		List<LoginLog> loginLogList = pager.getPagerList();
		Integer count = pager.getTotalRow();
//		return getTableJsonString(JsonMapper.toJsonString(loginLogList),count);
		return getTableJsonVO(loginLogList, count);
		} catch (Exception e) {
		}
//		return getTableJsonString("[]",0);
		return getTableJsonVO(new ArrayList<LoginLog>(), 0);
	}

}
